package com.acxy.ants.core;

import com.acxy.ants.common.kit.StrKit;
import com.acxy.ants.core.kit.LogKit;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.tomcat.util.descriptor.web.FilterDef;
import org.apache.tomcat.util.descriptor.web.FilterMap;

import javax.servlet.ServletException;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-10-26
 */
public class JTomcat {

    /**
     * 配置内嵌Tomcat启动
     *
     * @param target
     * @param port
     * @param name
     */
    public static void start(String target, Integer port, String name) {
        if (port == null) port = 8080;
        if (port < 0 || port > 65535) {
            throw new IllegalArgumentException("Invalid port of web server: " + port);
        }
        if (name.isEmpty()) {
            throw new IllegalStateException("Invalid name of web server: " + name);
        }

        name = StrKit.setInitial(name, "/");

        Tomcat tomcat = new Tomcat();
        // 主机名，或ip
        //tomcat.setHostname("localhost");
        // 设置端口，80为默认端口
        tomcat.setPort(port);
        // tomcat用于存储自身的信息，可以随意指定，最好包含在项目目录下
        tomcat.setBaseDir(".");

        Connector connector = tomcat.getConnector();
        Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
        //设置最大连接数
        protocol.setMaxConnections(2000);
        //设置最大线程数
        protocol.setMaxThreads(2000);
        protocol.setConnectionTimeout(30000);

        //建立server参照tomcat文件结构
        StandardServer server = (StandardServer) tomcat.getServer();
        AprLifecycleListener listener = new AprLifecycleListener();
        server.addLifecycleListener(listener);

        // 将appBase设为本项目所在目录
        tomcat.getHost().setAppBase(".");
        // tomcat.getHost().setAppBase(
        //         System.getProperty("user.dir") + File.separator + ".");
        // 第二个参数对应docBase为web应用路径，目录下应有WEB-INF,WEB-INF下要有web.xml
        try {

            Context ctx = tomcat.addWebapp(name, target == null ? "." : target);

            //添加Servelt
//        tomcat.addServlet(ctx, "bigsense", new TestServlet());
//        ctx.addServletMapping("/api.do", "bigsense");

            FilterDef fd = new FilterDef();
            fd.setFilterName("AntsFilter");
            fd.setFilter(new AntsFilter());
            ctx.addFilterDef(fd);

            FilterMap fm = new FilterMap();
            fm.setFilterName("AntsFilter");
            fm.addURLPattern("/*");
            ctx.addFilterMap(fm);
            // 启动tomcat
            tomcat.start();
            LogKit.info(Jetty.class, "Tomcat is start success!");
            // 维持
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }

    }


    public static void start(Integer port, String name) {
        start(null, port, name);
    }
}
